package com.vaadin.lab6;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class Lab extends Application {

    private Form form;

	@Override
    public void init() {
        Window mainWindow = new Window("Lab", new FormLayout());
        mainWindow.getContent().setSizeUndefined();
        setMainWindow(mainWindow);
        final Item item = createItem();

        PopupView popup = new PopupView(new PopupView.Content() {

            @Override
            public Component getPopupComponent() {
                final Table table = new Table();
                table.setPageLength(5);
                table.addContainerProperty("id", String.class, "");
                table.addContainerProperty("value", String.class, "");
                Item item = form.getItemDataSource();
                for (Object id : item.getItemPropertyIds()) {
                	
					table.addItem(id);
					table.getContainerProperty(id, "id").setValue(id);
					
					Property p = item.getItemProperty(id);
					table.getContainerProperty(id, "value").setValue(p.getValue());
				}
                
                
                return table;
            }

            @Override
            public String getMinimizedValueAsHTML() {
                return "Show Item";
            }
        });
        
        
        
        form = new Form();
        form.setFormFieldFactory(new MyFieldFactory());        
        form.setItemDataSource(createItem());
        form.setImmediate(true);
        
        
        
        mainWindow.addComponent(form);
        mainWindow.addComponent(popup);
    }

    private static Item createItem() {
        PropertysetItem item = new PropertysetItem();

        item.addItemProperty("name", new ObjectProperty<String>(""));
        item.addItemProperty("price", new ObjectProperty<Double>(0.0));
        item.addItemProperty("options", new ObjectProperty<Set<String>>(
                new HashSet<String>(Arrays.asList("first"))));
        item.addItemProperty("available", new ObjectProperty<Date>(Calendar
                .getInstance().getTime()));

        return item;
    }
}
